package com.bms.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.bms.dao.BaseDao;
import com.bms.service.BaseService;

/**
 * Service实现类-基类
 * @author Lan
 *
 * @param <T> 实体对象
 * @param <PK> 主键
 */
public class BaseServiceImpl<T, PK extends Serializable> implements BaseService<T, PK> {
	
	private static final Logger logger = Logger.getLogger(BaseServiceImpl.class);
	
	private BaseDao<T, PK> baseDao;

	public BaseDao<T, PK> getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao<T, PK> baseDao) {
		this.baseDao = baseDao;
	}

	public void delete(T entity) {
		try {
			baseDao.delete(entity);
			logger.debug("成功删除数据[" + entity.getClass().getName() + "]");
		} catch (DataAccessException e) {
			logger.error("删除实体对象时出现异常", e);
		}
	}

	public void delete(PK id) {
		try {
			T entity = baseDao.load(id);
			baseDao.delete(entity);
			logger.debug("成功删除数据[" + entity.getClass().getName() + " -- ID=" + id + "]");
		} catch (DataAccessException e) {
			logger.error("删除[ID=" + id + "]实体对象时出现异常", e);
		}
	}

	public void delete(PK[] ids) {
		for(PK id : ids){
			this.delete(id);
		}
	}

	public T get(PK id) {
		return baseDao.get(id);
	}

	public List<T> getAll() {
		return baseDao.getAll();
	}

	public T load(PK id) {
		try {
			return baseDao.load(id);
		} catch (DataAccessException e) {
			logger.error("获取ID=" + id + "数据失败", e);
			return null;
		}
	}

	public PK save(T entity) {
		return baseDao.save(entity);
	}

	public void update(T entity) {
		baseDao.update(entity);
	}
	
	
}
